Skip to content

Commit 0e46743

Browse files
issue/275 Add Support For Dynamic Imports (#286)
* task: add dynamic imports * fix: shelf init * fix: remove redundant async * fix: change chunkfile naming * fix: fix lint errors * fix: test eslint import exception * fix: add babel-eslint, update babel and eslintrc * fix: babel deps all 7.8.3 * fix: update tests for additional script elem * refactor babel-eslint implementation * remove extra formatting * update specs to reflect code splitting * refactoring script testing * enabling all smoke tests * refactor script testing * move webpack optimization to prod config * rollback webpack optimization Co-authored-by: Owen Buckley <[email protected]>
1 parent d35d0fe commit 0e46743

File tree

12 files changed

+748
-537
lines changed

12 files changed

+748
-537
lines changed

.eslintrc.js

+11-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
1+
const path = require('path');
2+
13
module.exports = {
4+
parser: 'babel-eslint',
25
parserOptions: {
36
ecmaVersion: 2018,
4-
sourceType: 'module'
7+
sourceType: 'module',
8+
babelOptions: {
9+
configFile: path.join(__dirname, './packages/cli/src/config/babel.config.js')
10+
}
511
},
612
env: {
713
browser: true,
@@ -141,7 +147,10 @@ module.exports = {
141147
'id-length': 0,
142148
'indent': [2, 2, {
143149
'VariableDeclarator': 1,
144-
'SwitchCase': 1
150+
'SwitchCase': 1,
151+
'ignoredNodes': [
152+
'TemplateLiteral'
153+
]
145154
}],
146155
'key-spacing': [2, {
147156
'beforeColon': false,
@@ -152,7 +161,6 @@ module.exports = {
152161
'max-nested-callbacks': [2, { 'maximum': 8 }],
153162
'new-cap': 2,
154163
'new-parens': 2,
155-
'newline-after-var': 2,
156164
'no-array-constructor': 2,
157165
'no-continue': 0,
158166
'no-inline-comments': 0,

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"test:tdd": "yarn test --watch"
2525
},
2626
"devDependencies": {
27+
"babel-eslint": "^10.0.3",
2728
"chai": "^4.2.0",
2829
"eslint": "^6.8.0",
2930
"jsdom": "^14.0.0",

packages/cli/package.json

+5-4
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,11 @@
2525
"access": "public"
2626
},
2727
"dependencies": {
28-
"@babel/core": "^7.6.0",
29-
"@babel/plugin-transform-runtime": "^7.6.2",
30-
"@babel/preset-env": "^7.6.0",
31-
"@babel/runtime": "^7.7.2",
28+
"@babel/core": "^7.8.3",
29+
"@babel/plugin-syntax-dynamic-import": "^7.8.3",
30+
"@babel/plugin-transform-runtime": "^7.8.3",
31+
"@babel/preset-env": "^7.8.3",
32+
"@babel/runtime": "^7.8.3",
3233
"@webcomponents/webcomponentsjs": "^2.3.0",
3334
"babel-loader": "^8.0.5",
3435
"chalk": "^2.4.2",

packages/cli/src/config/babel.config.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,10 @@ module.exports = {
3131
// https://github.com/babel/babel/issues/8829#issuecomment-456524916
3232
plugins: [
3333
[
34-
'@babel/plugin-transform-runtime', {
35-
regenerator: true
36-
}
34+
'@babel/plugin-transform-runtime', {
35+
regenerator: true
36+
},
37+
'@babel/plugin-syntax-dynamic-import'
3738
]
3839
]
3940

packages/cli/src/config/webpack.config.common.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ const getUserWorkspaceDirectories = (source) => {
1212
const mapUserWorkspaceDirectories = (directoryPath, userWorkspaceDirectory) => {
1313
const directoryName = directoryPath.replace(`${userWorkspaceDirectory}/`, '');
1414
const userWorkspaceDirectoryRoot = userWorkspaceDirectory.split('/').slice(-1);
15-
15+
1616
return new webpack.NormalModuleReplacementPlugin(
1717
// https://github.com/ProjectEvergreen/greenwood/issues/132
1818
new RegExp(`\\.\\.\\/${directoryName}.+$(?<!\.js)|${userWorkspaceDirectoryRoot}\\/${directoryName}.+$(?<!\.js)`),
1919
(resource) => {
20-
20+
2121
// workaround to ignore cli/templates default imports when rewriting
2222
if (!new RegExp('\/cli\/templates').test(resource.content)) {
2323
resource.request = resource.request.replace(new RegExp(`\\.\\.\\/${directoryName}`), directoryPath);
@@ -87,6 +87,7 @@ module.exports = ({ config, context }) => {
8787
output: {
8888
path: path.join(context.publicDir, '.', config.publicPath),
8989
filename: '[name].[hash].bundle.js',
90+
chunkFilename: '[name].[hash].bundle.js',
9091
publicPath: config.publicPath
9192
},
9293

packages/cli/test/cases/build.config.meta/build.config.meta.spec.js

+10-3
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ const expect = require('chai').expect;
3737
const runSmokeTest = require('../../../../../test/smoke-test');
3838
const TestBed = require('../../../../../test/test-bed');
3939

40+
const mainBundleScriptRegex = /index.*.bundle\.js/;
41+
4042
describe('Build Greenwood With: ', function() {
4143
const LABEL = 'Custom Meta Configuration and Nested Workspace';
4244
const meta = greenwoodConfig.meta;
@@ -80,10 +82,15 @@ describe('Build Greenwood With: ', function() {
8082
expect(title).to.be.equal('My Custom Greenwood App');
8183
});
8284

83-
it('should have one <script> tag in the <body>', function() {
84-
const scriptTag = dom.window.document.querySelectorAll('body script');
85+
it('should have one <script> tag in the <body> for the main bundle', function() {
86+
const scriptTags = dom.window.document.querySelectorAll('body script');
87+
const bundledScript = Array.prototype.slice.call(scriptTags).filter(script => {
88+
const src = script.src.replace('file:///', '');
89+
90+
return mainBundleScriptRegex.test(src);
91+
});
8592

86-
expect(scriptTag.length).to.be.equal(1);
93+
expect(bundledScript.length).to.be.equal(1);
8794
});
8895

8996
it('should have a router outlet tag in the <body>', function() {

packages/cli/test/cases/build.config.title/build.config.title.spec.js

+11-4
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ const expect = require('chai').expect;
2727
const runSmokeTest = require('../../../../../test/smoke-test');
2828
const TestBed = require('../../../../../test/test-bed');
2929

30+
const mainBundleScriptRegex = /index.*.bundle\.js/;
31+
3032
describe('Build Greenwood With: ', function() {
3133
const LABEL = 'Custom Title Configuration and Default Workspace';
3234
let setup;
@@ -63,12 +65,17 @@ describe('Build Greenwood With: ', function() {
6365
});
6466

6567
// rest of index smoke-test because <title></title> is changed for this case
66-
it('should have one <script> tag in the <body>', function() {
67-
const scriptTag = dom.window.document.querySelectorAll('body script');
68+
it('should have one <script> tag in the <body> for the main bundle', function() {
69+
const scriptTags = dom.window.document.querySelectorAll('body script');
70+
const bundledScript = Array.prototype.slice.call(scriptTags).filter(script => {
71+
const src = script.src.replace('file:///', '');
6872

69-
expect(scriptTag.length).to.be.equal(1);
70-
});
73+
return mainBundleScriptRegex.test(src);
74+
});
7175

76+
expect(bundledScript.length).to.be.equal(1);
77+
});
78+
7279
it('should have a router outlet tag in the <body>', function() {
7380
const outlet = dom.window.document.querySelectorAll('body eve-app');
7481

packages/cli/test/cases/build.default.workspace-template-app/build.default.workspace-template-app.spec.js

+10-3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ const { JSDOM } = require('jsdom');
2222
const path = require('path');
2323
const TestBed = require('../../../../../test/test-bed');
2424

25+
const mainBundleScriptRegex = /index.*.bundle\.js/;
26+
2527
describe('Build Greenwood With: ', function() {
2628
const LABEL = 'Default Greenwood Configuration and Workspace w/Custom App Template';
2729
let setup;
@@ -55,10 +57,15 @@ describe('Build Greenwood With: ', function() {
5557
expect(title).to.be.equal('Greenwood App');
5658
});
5759

58-
it('should have one <script> tag in the <body>', function() {
59-
const scriptTag = dom.window.document.querySelectorAll('body script');
60+
it('should have one <script> tag in the <body> for the main bundle', function() {
61+
const scriptTags = dom.window.document.querySelectorAll('body script');
62+
const bundledScript = Array.prototype.slice.call(scriptTags).filter(script => {
63+
const src = script.src.replace('file:///', '');
64+
65+
return mainBundleScriptRegex.test(src);
66+
});
6067

61-
expect(scriptTag.length).to.be.equal(1);
68+
expect(bundledScript.length).to.be.equal(1);
6269
});
6370

6471
it('should have a router outlet tag in the <body>', function() {

test/smoke-test.js

+13-10
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ const glob = require('glob-promise');
1212
const { JSDOM } = require('jsdom');
1313
const path = require('path');
1414

15+
const mainBundleScriptRegex = /index.*.bundle\.js/;
16+
1517
function publicDirectory(label) {
1618
describe(`Running Smoke Tests: ${label}`, function() {
1719
describe('Public Directory Generated Output', function() {
@@ -43,15 +45,15 @@ function defaultNotFound(label) {
4345
dom = await JSDOM.fromFile(path.resolve(this.context.publicDir, '404.html'));
4446
});
4547

46-
it('should have one bundle <script> tag in the <body>', function() {
48+
it('should have one <script> tag in the <body> for the main bundle', function() {
4749
const scriptTags = dom.window.document.querySelectorAll('body script');
48-
const bundleScripts = Array.prototype.slice.call(scriptTags).filter(script => {
49-
const src = script.src;
50+
const bundledScript = Array.prototype.slice.call(scriptTags).filter(script => {
51+
const src = script.src.replace('file:///', '');
5052

51-
return src.indexOf('index.') >= 0 && src.indexOf('.bundle.js') >= 0;
53+
return mainBundleScriptRegex.test(src);
5254
});
5355

54-
expect(bundleScripts.length).to.be.equal(1);
56+
expect(bundledScript.length).to.be.equal(1);
5557
});
5658

5759
it('should have a <title> tag in the <head>', function() {
@@ -86,15 +88,15 @@ function defaultIndex(label) {
8688
expect(title).to.be.equal('Greenwood App');
8789
});
8890

89-
it('should have one <script> tag for loading main JavaScript bundle at the end of the <body> tag', function() {
91+
it('should have one <script> tag in the <body> for the main bundle', function() {
9092
const scriptTags = dom.window.document.querySelectorAll('body eve-app ~ script');
91-
const bundleScripts = Array.prototype.slice.call(scriptTags).filter(script => {
92-
const src = script.src;
93+
const bundledScript = Array.prototype.slice.call(scriptTags).filter(script => {
94+
const src = script.src.replace('file:///', '');
9395

94-
return src.indexOf('index') >= 0 && src.indexOf('bundle') >= 0;
96+
return mainBundleScriptRegex.test(src);
9597
});
9698

97-
expect(bundleScripts.length).to.be.equal(1);
99+
expect(bundledScript.length).to.be.equal(1);
98100
});
99101

100102
it('should have a router outlet tag in the <body>', function() {
@@ -138,6 +140,7 @@ function defaultHelloPage(label) {
138140
it('should output a hello page directory', function() {
139141
expect(fs.existsSync(path.join(this.context.publicDir, './hello'))).to.be.true;
140142
});
143+
141144
it('should output an index.html file within the default hello page directory', function() {
142145
expect(fs.existsSync(path.join(this.context.publicDir, './hello', './index.html'))).to.be.true;
143146
});

www/components/shelf/shelf.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,22 @@ class shelf extends LitElement {
77

88
static get properties() {
99
return {
10-
shelfList: {
11-
type: Array
10+
list: {
11+
type: Function
1212
}
1313
};
1414
}
1515

1616
constructor() {
1717
super();
1818
this.selectedIndex = '';
19+
this.list = () => [];
1920
this.shelfList = [];
2021
}
2122

22-
connectedCallback() {
23+
async connectedCallback() {
2324
super.connectedCallback();
25+
this.shelfList = await this.list();
2426
this.collapseAll();
2527
this.expandRoute(window.location.pathname);
2628
}

www/templates/page-template.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ class PageTemplate extends LitElement {
2626
let list = [];
2727

2828
if (url.indexOf('/about') >= 0) {
29-
list = require('../components/shelf/about.json');
29+
list = () => import(/* webpackChunkName: 'about' */ '../components/shelf/about.json').then(({ default: data }) => data);
3030
} else if (url.indexOf('/docs') >= 0) {
31-
list = require('../components/shelf/documentation-list.json');
31+
list = () => import(/* webpackChunkName: 'documentation-list' */ '../components/shelf/documentation-list.json').then(({ default: data }) => data);
3232
} else if (url.indexOf('/getting-started') >= 0) {
33-
list = require('../components/shelf/getting-started-list.json');
33+
list = () => import(/* webpackChunkName: 'getting-started' */ '../components/shelf/getting-started-list.json').then(({ default: data }) => data);
3434
} else if (url.indexOf('/plugins') >= 0) {
35-
list = require('../components/shelf/plugins.json');
35+
list = () => import(/* webpackChunkName: 'plugins' */ '../components/shelf/plugins.json').then(({ default: data }) => data);
3636
}
3737

3838
this.shelfList = list;
@@ -48,7 +48,7 @@ class PageTemplate extends LitElement {
4848
<eve-header></eve-header>
4949
<div class='content-wrapper'>
5050
<div class="sidebar">
51-
<eve-shelf .shelfList="${this.shelfList}"></eve-shelf>
51+
<eve-shelf .list="${this.shelfList}"></eve-shelf>
5252
</div>
5353
<div class="content">
5454
<eve-container fluid>

0 commit comments

Comments
 (0)