-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(core): remove pure markers from js module output (#2936)
* test(webpack): check js module tree-shake * test(rollup): check js module tree-shake * test(esbuild): check js module tree-shake * chore: remove unneeded pure functions markers from generated js module
- Loading branch information
Showing
16 changed files
with
270 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
const { stylablePlugin } = require('@stylable/esbuild'); | ||
const { rmSync, existsSync } = require('node:fs'); | ||
const { join } = require('node:path'); | ||
|
||
module.exports.cssInJsDev = (build, options) => run('js', build, options); | ||
module.exports.cssBundleProd = (build, options) => run('css', build, options); | ||
|
||
function run(cssInjection, build, options) { | ||
const outdir = cssInjection === 'css' ? 'dist-bundle' : 'dist'; | ||
const path = join(__dirname, outdir); | ||
if (existsSync(path)) { | ||
rmSync(path, { recursive: true, force: true }); | ||
} | ||
return build({ | ||
...options({ | ||
entryPoints: ['./index'], | ||
plugins: [ | ||
stylablePlugin({ | ||
mode: cssInjection === 'css' ? 'production' : 'development', | ||
cssInjection, | ||
}), | ||
], | ||
}), | ||
outdir, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<meta http-equiv="X-UA-Compatible" content="IE=edge" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
<base href="/dist/" /> | ||
<title>tree-shake</title> | ||
</head> | ||
<body> | ||
<script type="module" src="index.js"></script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
import { classes } from './index.st.css'; | ||
document.body.innerHTML = `<div class="${classes.bbb}">bbb used</div>`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
.aaa {} | ||
|
||
.bbb {} | ||
|
||
.ccc { | ||
-st-states: stateX; | ||
container-name: containerX; | ||
} | ||
|
||
@property --propX; | ||
|
||
@keyframes keyframesX { | ||
0% { | ||
color: red; | ||
} | ||
|
||
100% { | ||
color: blue; | ||
} | ||
} | ||
|
||
:vars { | ||
varX: 1px; | ||
} | ||
|
||
@layer layerX; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
const { createNamespaceStrategyNode } = require('@stylable/node'); | ||
|
||
module.exports = { | ||
defaultConfig() { | ||
return { | ||
resolveNamespace: createNamespaceStrategyNode({ | ||
hashFragment: 'minimal', | ||
strict: true, | ||
}), | ||
}; | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { expect } from 'chai'; | ||
import { ESBuildTestKit } from '../esbuild-testkit'; | ||
|
||
describe('Stylable ESBuild plugin - tree-shake', () => { | ||
const tk = new ESBuildTestKit({ | ||
log: false, | ||
launchOptions: { | ||
headless: true, | ||
}, | ||
}); | ||
afterEach(() => tk.dispose()); | ||
|
||
it('should not include unused js', async () => { | ||
const { read } = await tk.build({ project: 'tree-shake', buildExport: 'cssBundleProd' }); | ||
const bundledJS = read('dist-bundle/index.js'); | ||
|
||
expect(bundledJS, 'keyframes').to.not.include('keyframesX'); | ||
expect(bundledJS, 'stVars').to.not.include('varX'); | ||
expect(bundledJS, 'vars').to.not.include('propX'); | ||
expect(bundledJS, 'layers').to.not.include('layerX'); | ||
expect(bundledJS, 'containers').to.not.include('containerX'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
import { classes } from './index.st.css'; | ||
document.body.innerHTML = `<div class="${classes.bbb}">bbb used</div>`; |
27 changes: 27 additions & 0 deletions
27
packages/rollup-plugin/test/projects/tree-shake/index.st.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
.aaa {} | ||
|
||
.bbb {} | ||
|
||
.ccc { | ||
-st-states: stateX; | ||
container-name: containerX; | ||
} | ||
|
||
@property --propX; | ||
|
||
@keyframes keyframesX { | ||
0% { | ||
color: red; | ||
} | ||
|
||
100% { | ||
color: blue; | ||
} | ||
} | ||
|
||
:vars { | ||
varX: 1px; | ||
} | ||
|
||
@layer layerX; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"name": "test-tree-shake", | ||
"version": "0.0.0-test" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { expect } from 'chai'; | ||
import { rollupRunner } from './test-kit/rollup-runner'; | ||
import { getProjectPath } from './test-kit/test-helpers'; | ||
|
||
describe('StylableRollupPlugin - tree-shake', function () { | ||
this.timeout(30000); | ||
|
||
const project = 'tree-shake'; | ||
|
||
const runner = rollupRunner({ | ||
projectPath: getProjectPath(project), | ||
entry: './index.js', | ||
pluginOptions: { | ||
stylableConfig(config) { | ||
config.mode = 'production'; | ||
// keep namespace with no hash for test expectations | ||
config.resolveNamespace = (namespace) => namespace; | ||
return config; | ||
}, | ||
includeGlobalSideEffects: true, | ||
}, | ||
rollupOptions: { | ||
treeshake: 'smallest', | ||
}, | ||
}); | ||
|
||
it('should not include unused js', async () => { | ||
const { ready, getOutputFiles } = runner; | ||
|
||
await ready; | ||
const outputFiles = getOutputFiles(); | ||
const bundledJS = outputFiles['index.js']; | ||
|
||
expect(bundledJS, 'keyframes').to.not.include('keyframesX'); | ||
expect(bundledJS, 'stVars').to.not.include('varX'); | ||
expect(bundledJS, 'vars').to.not.include('propX'); | ||
expect(bundledJS, 'layers').to.not.include('layerX'); | ||
expect(bundledJS, 'containers').to.not.include('containerX'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
packages/webpack-plugin/test/e2e/projects/tree-shake/src/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
import { classes } from './index.st.css'; | ||
document.body.innerHTML = `<div class="${classes.bbb}">bbb used</div>`; |
26 changes: 26 additions & 0 deletions
26
packages/webpack-plugin/test/e2e/projects/tree-shake/src/index.st.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
.aaa {} | ||
|
||
.bbb {} | ||
|
||
.ccc { | ||
-st-states: stateX; | ||
container-name: containerX; | ||
} | ||
|
||
@property --propX; | ||
|
||
@keyframes keyframesX { | ||
0% { | ||
color: red; | ||
} | ||
|
||
100% { | ||
color: blue; | ||
} | ||
} | ||
|
||
:vars { | ||
varX: 1px; | ||
} | ||
|
||
@layer layerX; |
30 changes: 30 additions & 0 deletions
30
packages/webpack-plugin/test/e2e/projects/tree-shake/webpack.config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
const { StylableWebpackPlugin } = require('@stylable/webpack-plugin'); | ||
const HtmlWebpackPlugin = require('html-webpack-plugin'); | ||
|
||
/** @type {import('webpack').Configuration} */ | ||
module.exports = { | ||
mode: 'production', | ||
context: __dirname, | ||
devtool: 'source-map', | ||
plugins: [ | ||
new StylableWebpackPlugin({ | ||
optimize: { | ||
removeUnusedComponents: true, | ||
removeComments: true, | ||
classNameOptimizations: false, | ||
shortNamespaces: false, | ||
removeEmptyNodes: true, | ||
minify: true, | ||
}, | ||
stylableConfig(config) { | ||
return { | ||
...config, | ||
resolveNamespace(namespace) { | ||
return namespace; | ||
}, | ||
}; | ||
}, | ||
}), | ||
new HtmlWebpackPlugin(), | ||
], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { StylableProjectRunner } from '@stylable/e2e-test-kit'; | ||
import { expect } from 'chai'; | ||
import { dirname } from 'path'; | ||
|
||
const project = 'tree-shake'; | ||
const projectDir = dirname( | ||
require.resolve(`@stylable/webpack-plugin/test/e2e/projects/${project}/webpack.config`) | ||
); | ||
|
||
describe(`(${project})`, () => { | ||
const projectRunner = StylableProjectRunner.mochaSetup( | ||
{ | ||
projectDir, | ||
launchOptions: { | ||
// headless: false, | ||
}, | ||
}, | ||
before, | ||
afterEach, | ||
after | ||
); | ||
|
||
it('should inline classes and remove the stylesheet js module', () => { | ||
const files = projectRunner.getProjectFiles(); | ||
expect(files['dist/main.js']).to.eql( | ||
`(()=>{"use strict";document.body.innerHTML='<div class="index__bbb">bbb used</div>'})();\n//# sourceMappingURL=main.js.map` | ||
); | ||
}); | ||
}); |