Skip to content

Commit

Permalink
perf(dicts): only load necessary styles
Browse files Browse the repository at this point in the history
  • Loading branch information
crimx committed Dec 3, 2018
1 parent 840c085 commit e15fbdd
Show file tree
Hide file tree
Showing 10 changed files with 25 additions and 14 deletions.
16 changes: 13 additions & 3 deletions config/webpack.config.prod.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ const entries = fs.readdirSync(paths.appSrc)

const entriesWithHTML = entries.map(({name, dirPath}) => ({name, dirPath, template: path.join(dirPath, 'index.html')}))
.filter(({template}) => fs.existsSync(template))

// add dictionary styles
const dictStyleEntries = {}
fs.readdirSync(path.join(paths.appSrc, 'components/dictionaries')).forEach(name => {
if (name === 'helpers.ts') { return }
dictStyleEntries[`dicts/${name}`] = path.join(paths.appSrc, 'components/dictionaries', name, '_style.scss')
})

// This is the production configuration.
// It compiles slowly and is focused on producing a fast and minimal bundle.
// The development configuration is different and lives in a separate file.
Expand All @@ -73,11 +81,13 @@ module.exports = {
// In production, we only want to load the polyfills and the app code.
entry: entries.reduce((result, {name, dirPath}) => {
const names = fs.readdirSync(dirPath)
const indexFile = names.find(name => /index\.((t|j)sx?)$/.test(name))
const indexFile = names.find(name => /index\.(ts|tsx|js|jsx|css|scss)$/.test(name))
if (!indexFile) { throw new Error(`Missing entry file for ${dirPath}`) }
result[name] = [require.resolve('./polyfills'), path.join(dirPath, indexFile)]
result[name] = indexFile.endsWith('css')
? path.join(dirPath, indexFile)
: [require.resolve('./polyfills'), path.join(dirPath, indexFile)]
return result
}, {}),
}, dictStyleEntries),
output: {
// The build folder.
path: paths.appBuild,
Expand Down
6 changes: 5 additions & 1 deletion scripts/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,11 @@ function generateByBrowser () {
filter: file => !/[\\\/]+\./.test(file),
}),
// project files
...files.map(file => fs.copy(file.path, path.join(dest, file.name)))
...files.map(file => fs.copy(file.path, path.join(dest, file.name), {
dereference: true,
// remove js files for css only chunks
filter: file => !/[\\\/]+(panel(-internal)?|dicts[\\\/]+[^\\\/]+)\.js(\.map)?$/.test(file)
}))
])
})).then(() => Promise.all(files.map(file =>
// clean up files
Expand Down
1 change: 1 addition & 0 deletions src/components/dictionaries/google/_style.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@import '../../MachineTrans/_style';
1 change: 1 addition & 0 deletions src/components/dictionaries/liangan/_style.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@import '../guoyu/_style';
1 change: 1 addition & 0 deletions src/components/dictionaries/sogou/_style.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@import '../../MachineTrans/_style';
3 changes: 3 additions & 0 deletions src/content/components/DictPanel/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,9 @@ export class DictPanel extends React.Component<DictPanelProps & { t: Translation
})
})}
</div>
{dictionaries.active.map(id =>
<link key={id} rel='stylesheet' href={browser.runtime.getURL(`/dicts/${id}.css`)} />
)}
</div>
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ h5, .h5, h6, .h6 {
font-size: 0.83em;
}

@import '../panel/panel';
@import '../panel/index'
4 changes: 0 additions & 4 deletions src/panel-internal/index.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/panel/panel.scss → src/panel/index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,4 @@
@import '../content/components/MenuBar/style';
@import '../content/components/DictItem/style';

@import '../components/MachineTrans/style';
@import '../components/Speaker/style';
4 changes: 0 additions & 4 deletions src/panel/index.ts

This file was deleted.

0 comments on commit e15fbdd

Please sign in to comment.