Skip to content

Commit 0d96f4f

Browse files
Split eval'ed es6 loader into mapped other script for compatible rollup builds; add production tests to help debug canjs tests
1 parent 86f3a7f commit 0d96f4f

11 files changed

+105
-36
lines changed

build-test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
var stealTools = require("steal-tools");
2+
3+
stealTools.build({
4+
main: "can-import-module/test",
5+
config: __dirname + "/package.json!npm"
6+
},{
7+
minify: false,
8+
debug: true,
9+
bundleAssets: true
10+
});

build.js

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,29 @@
11
"use strict";
22
var stealTools = require("steal-tools");
3+
var path = require("path");
4+
5+
function ignorer(path) {
6+
return path.indexOf("can-import-module") < 0;
7+
}
38

49
stealTools.export({
510
steal: {
6-
config: __dirname + "/package.json!npm"
11+
config: __dirname + "/package.json!npm",
12+
paths: {
13+
"can-import-module/loader/es6.js": "compat-loader/es6.js"
14+
}
715
},
816
outputs: {
9-
"+cjs": {},
10-
"+amd": {},
17+
"+bundled-cjs": {
18+
modules: ["can-import-module/can-import-module"],
19+
dest: path.join(__dirname, "dist", "cjs", "can-import-module.js"),
20+
ignore: ignorer
21+
},
22+
"+bundled-amd": {
23+
modules: ["can-import-module/can-import-module"],
24+
dest: path.join(__dirname, "dist", "amd", "can-import-module.js"),
25+
ignore: ignorer
26+
},
1127
"+global-js": {}
1228
}
1329
}).catch(function(e){

can-import-module.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ function preset(preset){
5959
addLoader(require("./loader/system"));
6060
break;
6161
case "ES2020":
62+
case "es2020":
6263
case "dynamic-import":
6364
addLoader(require("./loader/es6"));
6465
break;

compat-loader/es6.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
var getGlobal = require("can-globals/global/global");
2+
3+
// check for `noModule` in HTMLScriptElement. if its present, then the browser can handle dynamic loading because if
4+
// HTMLScriptElement.noModule is `true` the browser used to run fallback scripts in older browsers that do not support JavaScript modules
5+
if ("HTMLScriptElement" in getGlobal() && "noModule" in HTMLScriptElement.prototype) {
6+
// "import()" is a syntax error on some platforms and will cause issues if this module is bundled
7+
// into a larger script bundle, so only eval it to code if the platform is known to support it.
8+
module.exports = new Function("function esImport(moduleName) {\n" +
9+
// if moduleName has no extension, treat it as a javascript file and add .js extension
10+
"if (!(moduleName.match(/[^\\\/]\.([^.\\\/]+)$/) || [null]).pop()) {\n" +
11+
"moduleName += '.js';\n" +
12+
"}\n" +
13+
"return import(moduleName.replace(/['\"]+/g, ''));\n" +
14+
"}");
15+
} else {
16+
module.exports = function() {}
17+
}

loader/es6.js

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,13 @@
11
var getGlobal = require("can-globals/global/global");
22

3-
// check for `noModule` in HTMLScriptElement. if its present, then the browser can handle dynamic loading because if
4-
// HTMLScriptElement.noModule is `true` the browser used to run fallback scripts in older browsers that do not support JavaScript modules
5-
if ("HTMLScriptElement" in getGlobal() && "noModule" in HTMLScriptElement.prototype) {
6-
// "import()" is a syntax error on some platforms and will cause issues if this module is bundled
7-
// into a larger script bundle, so only eval it to code if the platform is known to support it.
8-
module.exports = new Function("function esImport(moduleName) {\n" +
9-
// if moduleName has no extension, treat it as a javascript file and add .js extension
10-
"if (!(moduleName.match(/[^\\\/]\.([^.\\\/]+)$/) || [null]).pop()) {\n" +
11-
"moduleName += '.js';\n" +
12-
"}\n" +
13-
"return import(moduleName.replace(/['\"]+/g, ''));\n" +
14-
"}");
15-
} else {
16-
module.exports = function() {
17-
throw new Error("ES dynamic imports are not available on this platform");
3+
module.exports = function(moduleName) {
4+
// check for `noModule` in HTMLScriptElement. if its present, then the browser can handle dynamic loading because if
5+
// HTMLScriptElement.noModule is `true` the browser used to run fallback scripts in older browsers that do not support JavaScript modules
6+
if ("HTMLScriptElement" in getGlobal() && "noModule" in HTMLScriptElement.prototype) {
7+
// if moduleName has no extension, threat it as a javascript file and add .js extension
8+
if (!(moduleName.match(/[^\\\/]\.([^.\\\/]+)$/) || [null]).pop()) {
9+
moduleName += '.js';
10+
}
11+
return import(moduleName.replace(/['"]+/g, ''));
1812
}
19-
}
13+
};

package.json

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,19 @@
1313
"url": ""
1414
},
1515
"scripts": {
16+
"build": "node build.js",
17+
"build:test": "node build-test.js",
1618
"preversion": "npm test",
1719
"postpublish": "git push --tags && git push",
1820
"testee": "testee test.html --browsers firefox",
21+
"testee:production": "testee test.html --browsers firefox",
1922
"mocha": "mocha -- test/node-test.js",
2023
"test": "npm run jshint && npm run mocha && npm run testee",
24+
"test:production": "npm run build:test && npm run testee:production",
2125
"jshint": "jshint ./*.js --config",
22-
"release:patch": "npm version patch && npm publish",
23-
"release:minor": "npm version minor && npm publish",
24-
"release:major": "npm version major && npm publish",
26+
"release:patch": "npm run build && npm version patch && npm publish",
27+
"release:minor": "npm run build && npm version minor && npm publish",
28+
"release:major": "npm run build && npm version major && npm publish",
2529
"release:pre": "npm version prerelease && npm publish --tag=pre",
2630
"develop": "done-serve --static --develop --port 8080"
2731
},
@@ -32,13 +36,14 @@
3236
],
3337
"devDependencies": {
3438
"assert": "^2.0.0",
39+
"can-import-module-test": "file:./test",
3540
"jshint": "^2.9.1",
3641
"mocha": "^9.1.3",
37-
"steal": "^2.2.1",
38-
"steal-qunit": "^1.0.1",
39-
"steal-tools": "^2.2.1",
40-
"testee": "^0.9.0",
41-
"can-import-module-test": "file:./test"
42+
"steal": "^2.3.0",
43+
"steal-css": "^1.3.2",
44+
"steal-qunit": "^2.0.0",
45+
"steal-tools": "^2.3.0",
46+
"testee": "^0.9.0"
4247
},
4348
"license": "MIT",
4449
"dependencies": {

test-production.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<!DOCTYPE html>
2+
<title>CanJS production tests</title>
3+
<script src="dist/steal.production.js" main="can-import-module/test" env="canjs-test"></script>
4+
<div id="qunit-fixture"></div>

test.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1+
// preload for production tests
2+
import 'can-import-module/test/cjs-module';
3+
14
import './test/browser-test';

test/browser-test.js

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,23 @@
11
var QUnit = require('steal-qunit');
22
var moduleImport = require( '../can-import-module');
33

4+
var prefix;
5+
QUnit.module('can-import-module', {
6+
before: function() {
7+
if(QUnit.config.modules.length > 1) {
8+
prefix = {
9+
npm: "can-import-module/test",
10+
esm: "/node_modules/can-import-module/test"
11+
};
12+
} else {
13+
prefix = {
14+
npm: "~/test",
15+
esm: "/test"
16+
};
17+
}
18+
}
19+
})
20+
421
QUnit.test('load npm library via default config (SystemJS first)', function(assert) {
522
return moduleImport('can-import-module/test/cjs-module').then(function(data) {
623
assert.equal(data, 'Hello from cjs-module');
@@ -11,7 +28,7 @@ QUnit.test('load npm library via default config (SystemJS first)', function(asse
1128

1229
QUnit.test('load es6 module with dynamic import', function(assert) {
1330
moduleImport.preset('es2020');
14-
return moduleImport('/test/es6-module').then(function(module) {
31+
return moduleImport(prefix.esm + '/es6-module').then(function(module) {
1532
assert.equal(module.default, 'Hello from es6-module');
1633
}).then(null, function(err){
1734
assert.ok(false, err);
@@ -25,7 +42,7 @@ QUnit.test('custom loader', function(assert){
2542
return import(moduleName.replace('cjs', 'es6'));
2643
});
2744

28-
return moduleImport('/test/cjs-module.js').then(function(module) {
45+
return moduleImport(prefix.esm + '/cjs-module.js').then(function(module) {
2946
assert.equal(module.default, 'Hello from es6-module', 'es6 module loaded');
3047
}).then(null, function(err){
3148
assert.ok(false, err);
@@ -34,7 +51,7 @@ QUnit.test('custom loader', function(assert){
3451

3552
QUnit.test('presets', function(assert){
3653
moduleImport.preset('node');
37-
return moduleImport('/test/cjs-module.js').then(function() {
54+
return moduleImport(prefix.npm + '/cjs-module.js').then(function() {
3855
assert.ok(false);
3956
}, function(err){
4057
assert.equal(err, 'no proper module-loader available');

test/es6-module.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1+
'use strict';
2+
13
export default 'Hello from es6-module';

test/node-test.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,16 @@ describe('nodeJS', function (){
2525
});
2626

2727
describe('es6 module', function() {
28-
it('throws an error when trying to import in es6 style', function() {
28+
it('does not syntax error when trying to import in es6 style', function() {
2929
load.flushLoader();
3030
load.addLoader(require('../loader/es6'));
31-
return load('/test/es6-module').then(function() {
32-
assert.fail("es6 loader did not throw error when expected");
31+
return load('/test/es6-module').then(function(result) {
32+
assert.fail("es6 loader did not reject when expected");
3333
}, function(err) {
3434
assert.equal(
35-
err.message,
36-
'ES dynamic imports are not available on this platform',
37-
'Correct error message returned from import attempt'
35+
err,
36+
'no proper module-loader available',
37+
'Incorrect error message returned from import attempt: ' + err
3838
);
3939
});
4040
});

0 commit comments

Comments
 (0)